// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var css_change = require("./update_phone_change.css");
    var wait = 60;
    var changeMobileUuid;
    var mobile;
    var password;
    var appKey;
    var sessionUuid;
    var authenticode;
    var flag = false;
    var flag1 = false;
    var init = function(page){
    	password = page.params.password;
		initEvent();
		router.init();
    };
    
	var initEvent = function(){
		/*失去焦点时验证是否手机号码*/
		$("#main").on("blur", "#phoneNum", function() {
			var phoneNum = $(this).val();
			if(phoneNum == null || phoneNum ==""){
				base.alert(2,"请输入手机号码!");
			}else if(!validatePhoneNum(phoneNum)) {
				base.alert(2,"请输入正确的手机号码!");
				
				flag = false;
			}else{
				$("#errinfo1").addClass("none");
				flag = true;
			}
		});
	
		/*验证是否已输入验证码*/
		$("#main").on("blur", "#code input", function() {
			var code = $(this).val();
			if(code == null || code =="") {
				base.alert(2,"请输入验证码!");
				flag = false;
			}else if(code.length != 6){
				base.alert(2,"请输入正确的验证码!");
				flag = false;
			}else{
				$("#errinfo2").addClass("none");
				flag = true;
			}
		});
//		
		//获取验证码60秒内禁止再次获取
		$("#main").on("click", "#code a", function() {
			if(wait==60 && $("#phoneNum").val().trim()!=""){
				changeEnterpriseAccountNewMobile();
				time();
			}
			if(wait>0){
				$(this).unbind("click");
			}
		});
//		
		$("#main").on("click", "#phone_change_next", function() {
			if(flag){
				changeMobile();
			}
		});
    };
    
    //修改手机号
    var changeMobile = function() {
    	authenticode = $("#code input").val();
    	mobile = $("#phoneNum").val();
    	base.fnAjax({
            action: "validateEnterpriseChangeMobileUuid.json",
            oData: {
                mobile: mobile,
                changeMobileUuid: changeMobileUuid,
                authenticode: authenticode,
                password: password
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	router.route("updatePhone/update_phone_success")
                } else {
                	base.alert(3,data.message);
					flag = false;
                }
            }
        });
    }
    
    //登陆
    var login = function(){
		var mUsername = mobile;
		var mPassword = mobile.substring(5, mobile.length);
		base.fnAjax({
			action: "userNameLogin.json",
			version: "v3.0",
			async: false,
			oData: {
                username: mUsername,
                password: mPassword,
                appKey: "FINANCE-ENTERPRISE-WEB"
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	base.saveData("sid", data.datas[0].sessionUuid);//存sessionUuid
					base.saveData("appKey", data.datas[0].appUuid);//存appkey
					router.route("register/register_success.html");
                } else {
                    base.alert(3,"信息获取失败");
                    $('.error_info').css('visibility','visible');
                }
            }
		});
	};
    
    //限制60秒内不能重复获取验证码
    var time = function() {
    	if (wait == 0) {
			$("#code a").removeAttr("disabled");
			$("#code a").html("获取验证码");
			wait = 60;
		} else {
			$("#code a").unbind("click");
			$("#code a").attr("disabled", "true");
			$("#code a").html("重新发送(" + wait + ")");
			wait--;
			setTimeout(function() {
				time()
			},
			1000)
		}
    }
    
    //验证手机号码是否正确
    var validatePhoneNum = function(str) {
    	var regMobile = /^0?1[3|4|5|8][0-9]\d{8}$/ //手机
    	if(regMobile.test(str)) {
    		return true;
    	}else {
    		return false;
    	}
    }

	//获取验证码
	var changeEnterpriseAccountNewMobile = function(){
		mobile = $("#phoneNum").val();
        base.fnAjax({
            action: "changeEnterpriseAccountNewMobile.json",
            oData: {
                mobile: mobile,
                password: password
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	changeMobileUuid = data.datas[0].changeMobileUuid;
                } else {
                    base.alert(3,"信息获取失败");
                }
            }
        });
	}

    return {
        init:init
    };
});